package com.yuliang.utils.util;

import java.security.MessageDigest;

/**
 * @author Huangkai
 */
public class Sha1Utils {
    public static String shaEncode(String input) {
        try {
// 创建MessageDigest实例
            MessageDigest digest = MessageDigest.getInstance("SHA-1");

            // 将字符串转换为字节数组并计算其摘要
            byte[] inputBytes = input.getBytes();
            byte[] hashedBytes = digest.digest(inputBytes);

            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashedBytes) {
                String hex = Integer.toHexString(0xFF & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            String hashedData = hexString.toString();

            // 打印加密结果
            System.out.println("加密结果：" + hashedData);
            return hashedData;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return null;
    }
    public static void main(String[]args){
        System.out.println(shaEncode("777"));
    }
}
